package com.jkm.boss.controller.channel;

import com.google.common.collect.Lists;
import com.jkm.base.common.entity.CommonResponse;
import com.jkm.boss.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import com.jkm.boss.service.ProductService;
import com.jkm.boss.service.ProductChannelDetailService;
import com.jkm.boss.entity.Product;
import com.jkm.boss.entity.ProductChannelDetail;
import com.jkm.boss.helper.response.ProductListResponse;
import com.jkm.boss.enums.EnumProductType;


/**
 * Created by shitou on 17/4/17.
 */
@Controller
@RequestMapping(value = "/admin/product")
@Slf4j
public class ProductController extends BaseController{

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductChannelDetailService productChannelDetailService;

    /**
     * 产品列表
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public CommonResponse list() {

        final List<Product> list = this.productService.selectAll();
        if (list == null){
            return CommonResponse.objectResponse(1, "success", Collections.EMPTY_LIST);
        }
        final List<ProductListResponse> responseList = Lists.newArrayList();
        for (Product product : list){
            //根据产品查找产品详情
            final List<ProductChannelDetail> detailList = this.productChannelDetailService.selectByProductId(product.getId());
            for (ProductChannelDetail detail : detailList){
                detail.setProductMerchantPayRate(detail.getProductMerchantPayRate().multiply(new BigDecimal(100)).setScale(2));
                detail.setProductTradeRate(detail.getProductTradeRate().multiply(new BigDecimal(100)).setScale(2));
            }
            final ProductListResponse response = new ProductListResponse();
            if (product.getType().equals("hss")){
                response.setType(EnumProductType.HSS.getId());
            }
            if (product.getType().equals("hsy")){
                response.setType(EnumProductType.HSY.getId());
            }
            response.setProductId(product.getId());
            response.setProductName(product.getProductName());
            response.setAccountId(product.getAccountId());
            response.setLimitPayFeeRate(product.getLimitPayFeeRate().multiply(new BigDecimal(100)).setScale(2));
            response.setLimitWithdrawFeeRate(product.getLimitWithdrawFeeRate());
            response.setMerchantWithdrawType(product.getMerchantWithdrawType());
            response.setDealerBalanceType(product.getDealerBalanceType());
            response.setList(detailList);
            responseList.add(response);
        }
        return  CommonResponse.objectResponse(1, "success", responseList);
    }


}
